﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace sy1_6
{
    public partial class Form1 : Form
    {
        private Font originalFont;

        public Form1()
        {
            InitializeComponent();
            // 确保目标文本框可接收格式文本
            rtbTarget.ReadOnly = false;
            rtbTarget.ReadOnly = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            originalFont = rtbSource.Font;
            rtbSource.Text = "选中这段文字，点击复制按钮即可看到效果\n这是第二行示例文本";
        }

        private void btnFormat_Click(object sender, EventArgs e)
        {
            rtbSource.Font = new Font("隶书", 20);
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            rtbSource.Font = originalFont;
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if (rtbSource.SelectionLength == 0)
            {
                MessageBox.Show("请先选中要复制的内容！", "提示");
                return;
            }

            try
            {
                // 清空目标文本
                rtbTarget.Clear();

                // 获取选中的文本和字体
                string selectedText = rtbSource.SelectedText;
                Font selectedFont = rtbSource.SelectionFont ?? rtbSource.Font;

                // 直接在目标文本框中添加带格式的文本
                rtbTarget.SelectionStart = rtbTarget.TextLength;
                rtbTarget.SelectionFont = selectedFont;
                rtbTarget.SelectedText = selectedText;

                MessageBox.Show("复制成功，已显示在目标文本框", "成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("复制失败: " + ex.Message, "错误");
            }
        }
    }
}
